/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.job.task;

import cn.hutool.core.io.FileUtil;
import io.renren.common.exception.RRException;
import io.renren.common.utils.FTPUtils;
import io.renren.common.utils.FileUtils;
import io.renren.common.utils.TimeUtils;
import io.renren.modules.generator.service.FireForecastDataService;
import io.renren.modules.generator.util.FTPUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.InputStream;
import java.util.Date;

/**
 * 测试定时任务(演示Demo，可删除)
 *
 * testTask为spring bean的名称
 *
 * @author Mark 1942951600@qq.com
 */
@Component("testTask")
public class TestTask implements ITask {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private FTPUtils ftpUtils;
	@Autowired
	private FireForecastDataService fireForecastDataService;
	@Override
	public void run(String params){
//		try {
////
//			InputStream inputStream = ftpUtils.downloadFile("H08_20210807_0500_L2WLF010_FLDK.06001_06001.csv");
//			// 定义文件存储路径，并创建相关文件夹
//			String basePath = FileUtils.getRootPath();
//			System.out.println(basePath);
//			String fileName = "H08_20210807_0500_L2WLF010_FLDK.06001_06001.csv";
//
//			//文件后缀变大写
//			String suffix = fileName.substring(fileName.lastIndexOf(".")).toUpperCase();
//			String savePath = "/UploadFiles/"+ "/Temporary/" + TimeUtils.formatDate2String(new Date(),"yyyyMM/");
//			File fileDir = new File(basePath + savePath);
//			if (!fileDir.exists()) fileDir.mkdirs();
//			// 需要重新命名文件名
//
//			fileName = TimeUtils.formatDate2String(new Date(), "yyyyMMddHHmmssSSS") + suffix.toLowerCase();
//			System.out.println(fileName);
//			Thread.sleep(2);
//			if (inputStream == null){
//				throw new RRException("请再次发送请求");
//			}
//			FileUtil.writeFromStream(inputStream, fileName);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		try {
			String path = FTPUtil.genePath();
			InputStream inputStream = ftpUtils.downloadFile(path);
			// 定义文件存储路径，并创建相关文件夹
			String basePath = FileUtils.getRootPath();
			System.out.println(basePath);

			//文件后缀变大写
			String suffix = ".CSV";
			String savePath = "UploadFiles/"+ "Temporary/" + TimeUtils.formatDate2String(new Date(),"yyyyMM/");
			File fileDir = new File(basePath + savePath);
			//我这个项目这里有中文所以出现了很大的问题；

			if (!fileDir.exists()) fileDir.mkdirs();
			String fileName = TimeUtils.formatDate2String(new Date(), "yyyyMMddHHmmssSSS") + suffix.toLowerCase();
			System.out.println(fileName);
			Thread.sleep(2000);
			if (inputStream == null){
				throw new RRException("请再次发送请求");
			}
			String fullFilePath= basePath + savePath+fileName;
			System.out.println();
			FileUtil.writeFromStream(inputStream, fullFilePath);
			Thread.sleep(4);
			//这里开始进入解析csv
			fireForecastDataService.parseCSV(fullFilePath);
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.debug("TestTask定时任务正在执行，参数为：{}", params);
	}
}
